package com.meta.pic.view

import android.annotation.SuppressLint
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyHolder
import com.airbnb.epoxy.EpoxyModelClass
import com.airbnb.epoxy.EpoxyModelWithHolder
import com.bumptech.glide.Glide
import com.meta.pic.PicItem
import com.meta.pic.R
import com.meta.pic.util.RatioLayout


@SuppressLint("NonConstantResourceId")
@EpoxyModelClass(layout = R.layout.item_pic)
abstract class PicItemView : EpoxyModelWithHolder<PicItemView.Holder>() {

    @EpoxyAttribute
    lateinit var model: PicItem

    override fun bind(holder: Holder) {
        holder.init(model)
    }

    class Holder : EpoxyHolder() {
        private lateinit var imageView: ImageView
        private lateinit var textView: TextView
        private lateinit var rootView: RatioLayout
        override fun bindView(itemView: View) {
            imageView = itemView.findViewById(R.id.imageView)
            textView = itemView.findViewById(R.id.textView)
            rootView = itemView as RatioLayout
        }

        fun init(model: PicItem) {
            Glide.with(imageView).load(model.thumb).into(imageView)
            textView.text = model.title
            rootView.whRatio = model.ratio
        }
    }
}